package cn.wolfcode.web.controller;

import cn.wolfcode.query.CustomerReportQuery;
import cn.wolfcode.service.ICustomerReportService;
import cn.wolfcode.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportService customerReportService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerReportQuery qo){
        PageInfo<Map> pageInfo = customerReportService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return"/customerReport/list";

    }

    @RequestMapping("/bar")
    public String bar(Model model, @ModelAttribute("qo") CustomerReportQuery qo){
        //获取统计报表数据(不需要分页)
        List<HashMap> maps =  customerReportService.selectAll(qo);
        //根据柱状图页面可以知道需要传两个数据(x轴和y轴)
        //新建两个list集合用来封装所要查询的数据
        ArrayList<Object> xList = new ArrayList<>();
        ArrayList<Object> yList = new ArrayList<>();
        //遍历获取的统计数据
        for (HashMap map : maps) {
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }

        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        model.addAttribute("groupTypeName",MessageUtil.changMsg(qo));
        return"/customerReport/bar";

    }

    @RequestMapping("/pie")
    public String pie(Model model, @ModelAttribute("qo") CustomerReportQuery qo){
        //获取统计报表数据(不需要分页)
        List<HashMap> maps =  customerReportService.selectAll(qo);
        //根据柱状图页面可以知道需要传两个数据
        //新建两个list集合用来封装所要查询的数据
        ArrayList<Object> legendList = new ArrayList<>();
        ArrayList<Object> seriesList = new ArrayList<>();
        //遍历获取的统计数据
        for (HashMap map : maps) {
            legendList.add(map.get("groupType"));
            //新建一个map集合装柱状图要显示的数据
            HashMap<Object, Object> obj = new HashMap<>();
            obj.put("name",map.get("groupType"));
            obj.put("value",map.get("number"));
            seriesList.add(obj);
        }

        model.addAttribute("legendList", JSON.toJSONString(legendList));
        model.addAttribute("seriesList", JSON.toJSONString(seriesList));
        model.addAttribute("groupTypeName",MessageUtil.changMsg(qo));
        return"/customerReport/pie";

    }

}
